package com.x.erp.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.Notice;
import com.x.erp.model.NoticeMarket;
import com.x.erp.rpc.NoticeRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;

/**
 * 公告管理
 */

@RestController
public class NoticeController extends BaseController {

	@Reference
	private NoticeRpcService noticeRpcService;

	/**
	 * 查询公告信息开始时间结束时间包含今日
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getNoticeList", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel getNoticeList(NoticeMarket noticeMarket, Notice notice) {
		noticeMarket.setNotice(notice);
		noticeMarket.getNotice().setNoticeStartTime(this.getDate(this.getDate(this.getDate(0),"yyyy-MM-dd"),"yyyy-MM-dd"));
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功",
				noticeRpcService.selectNoticeMarketList(noticeMarket));
	}


	/**
	 * 查询管理所有公告信息
	 *
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getNoticeMarketList", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel getNoticeMarketList(NoticeMarket noticeMarket, Notice notice) {
		noticeMarket.setNotice(notice);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功",
		noticeRpcService.selectNoticeMarketList(noticeMarket));
	}



	/**
	 * 查询公告数量
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getNoticeListNum", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getNoticeListNum(NoticeMarket noticeMarket, Notice notice) {
		noticeMarket.setNotice(notice);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", noticeRpcService.selectNoticeMarketNum(noticeMarket));
	}

	/**
	 * 新增或者更新公告信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/insertOrUpdateNotice", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel insertOrUpdateNotice(NoticeMarket noticeMarket, Notice notice) {
		String marketId = noticeMarket.getMarketId();
		if (this.isNotNull(notice.getNoticeId())){
			noticeRpcService.updateNotice(notice,marketId);
		}else{
			noticeRpcService.insertNotice(notice,marketId);
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "操作成功");
	}

	/**
	 * 更新公告状态
	 * @param shop
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateNotice", method = {RequestMethod.GET, RequestMethod.POST})
	public ResultModel updateNotice(Notice notice) {
		if (StringUtils.isEmpty(notice.getNoticeId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "公告ID不能为空！");
		}
		noticeRpcService.updateNotice(notice);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "更新成功");
	}



	/**
	 * 修改公告信息
	 * @param notice
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getNoticeInfo", method = {RequestMethod.GET, RequestMethod.POST})
	public ResultModel getNoticeInfo(Notice notice, NoticeMarket noticeMarket) {
		if (StringUtils.isEmpty(notice.getNoticeId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "公告ID不能为空！");
		}
		if (StringUtils.isEmpty(noticeMarket.getMarketId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "市场ID不能为空！");
		}
		noticeMarket.setNoticeId(notice.getNoticeId());
		//查询有多少个 市场
//		int  noticeListNum = noticeRpcService.selectNoticeListNum(noticeMarket);
		List<NoticeMarket> noticeMarketList = noticeRpcService.selectNoticeMarketList(noticeMarket);
		List<Notice> noticeInfoList = noticeRpcService.selectNoticeList(notice);
		Map<String, Object> noticeInfo = new HashMap<>();
		if (noticeInfoList.size()>0){
			noticeInfo.put("notice",noticeInfoList.get(0));
		}
		//如果有1条返回市场id
		if (noticeMarketList.size() == 1){
			noticeInfo.put("marketId",noticeMarketList.get(0).getMarketId());
		}
		noticeInfo.put("marketNum", noticeMarketList.size());
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功",noticeInfo );
	}



}

